/**
 * 
 */
package com.baijia.tianxiao.push.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baijia.tianxiao.connect.route.api.enums.MsgType;
import com.baijia.tianxiao.connect.route.api.model.TxMessage;
import com.baijia.tianxiao.push.enums.PUshMessageBizName;
import com.baijia.tianxiao.push.result.TxMessageBaseResult;
import com.baijia.tianxiao.push.service.BizMessageService;
import com.baijia.tianxiao.push.service.UserDeviceAttributeService;

/**
 * @author leiruiqi
 *
 */

@Service("bizMessageService")
public class BizMessageServiceImpl implements BizMessageService {
	
	private static Logger logger = LoggerFactory.getLogger(BizMessageServiceImpl.class); 
	
	@Resource
	private UserDeviceAttributeService userDeviceAttributeService;
	

	/* (non-Javadoc)
	 * @see com.baijia.tianxiao.push.service.BizMessageService#bizMessage(com.baijia.tianxiao.push.model.PushTxMessage)
	 */
	@Override
	public TxMessage bizMessage(TxMessage receiveTxmessage) {
		// TODO Auto-generated method stub
		//receiveTxmessage
		return null;
	}

	/* (non-Javadoc)
	 * @see com.baijia.tianxiao.push.service.BizMessageService#bizMessageSyn(com.baijia.tianxiao.push.model.PushTxMessage)
	 */
	@Override
	public TxMessage bizMessageSyn(TxMessage receiveTxmessage) {
		String bizName = getBizName(receiveTxmessage.getAction());
		try {
			if (StringUtils.equals(bizName, PUshMessageBizName.addAttribute.name())) {
				TxMessage txMessageArk = doBizAddAttribute(receiveTxmessage);
				return txMessageArk;
			} else {
				TxMessage txMessageArk = createBizErrorMessage(receiveTxmessage, "action not support");
				return txMessageArk;
			} 
		} catch (Exception e) {
			logger.error("",e);
			TxMessage txMessageArk = createBizErrorMessage(receiveTxmessage, "sys exception");
			return txMessageArk;
		}
	}
	private TxMessage createBizErrorMessage(TxMessage receiveTxmessage,String errMsg){
		TxMessage txMessageArk = createDefaultArkTxMessage(receiveTxmessage);
		TxMessageBaseResult txMessageResult = new TxMessageBaseResult();
		txMessageResult.setSuccess(false);
		txMessageResult.setMsg(errMsg);
		txMessageArk.setData(JSON.toJSONString(txMessageResult));
		return txMessageArk;
	}
	private TxMessage doBizAddAttribute(TxMessage receiveTxmessage){
		
		TxMessage txMessageArk = createDefaultArkTxMessage(receiveTxmessage);
		
		boolean bool =userDeviceAttributeService.addAttribute(receiveTxmessage.getClientInfo().getBizType(), receiveTxmessage.getClientInfo().getUserId(), receiveTxmessage.getClientInfo().getDeviceId(), receiveTxmessage.getData());
		TxMessageBaseResult txMessageResult = new TxMessageBaseResult();
		
		if(bool){
			txMessageResult.setSuccess(true);
			
		}else{
			txMessageResult.setSuccess(false);
		}
		txMessageArk.setData(JSON.toJSONString(txMessageResult));
		return txMessageArk;
	}
	
	private TxMessage createDefaultArkTxMessage(TxMessage receiveTxmessage){
		TxMessage txMessageArk = new TxMessage();
		txMessageArk.setAction(receiveTxmessage.getAction());
		txMessageArk.setClientInfo(receiveTxmessage.getClientInfo());
		txMessageArk.setSignId(receiveTxmessage.getSignId());
		txMessageArk.setType(MsgType.bizArk.name());
		return txMessageArk;
	}
	
	
	private String getBizName(String action){
		String bizName = null;
		if(StringUtils.isNotBlank(action)){
			String[] actionsplitArray = null;
			try {
				actionsplitArray = action.split("-");
			} catch (Exception e) {
				return bizName;
			}
			if(actionsplitArray!=null&&actionsplitArray.length==2){
				//bizDomain = actionsplitArray[0];
				bizName  =actionsplitArray[1];
			}
		}
		return bizName;
	}

}
